package com.itheima.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.domian.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;

    //查询购物车
    @GetMapping("list")
    public R<List<ShoppingCart>> list() {
        //根据用户名获取到 对应的购物车
        Long id = BaseContext.getId();
        List<ShoppingCart> list = shoppingCartService.list(Wrappers.<ShoppingCart>lambdaQuery().eq(ShoppingCart::getUserId, id));
        return R.success(list);
    }

    //添加菜品或者套餐
    @PostMapping("add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart) {
        shoppingCartService.add(shoppingCart);
        return R.success("添加成功");
    }

    //删除套餐 或者菜品
    @PostMapping("sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart) {
        shoppingCartService.sub(shoppingCart);
        return R.success("删除成功");
    }

    //清空 购物车
    @DeleteMapping("clean")
    public R<String> delete() {
        shoppingCartService.delete();
        return R.success("清楚成功");
    }
}
